package com.zhongge.stackOJ;

import java.util.Stack;

/**
 * @ClassName Solution
 * @Description TODO 栈的压入、弹出序列
 * @Author 笨忠
 * @Date 2025/8/23 11:27
 * @Version 1.0
 */
public class Solution {
    public boolean isPopOrder (int[] pushV, int[] popV) {
        //辅助栈
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        int j = 0;

        //循环遍历pushV
        for (; i < pushV.length; i++) {
            //入栈
            stack.push(pushV[i]);

            //循环比较栈顶元素与popV中的j处值
            //瞄一眼栈顶元素如果相同再出栈并j++
            while (!stack.isEmpty() && j < popV.length && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }

        //pushV空后==》看栈中是否有元素
        return stack.isEmpty();
    }

    public static boolean isPopOrder2 (String pushV, String popV) {
        //辅助栈
        Stack<Character> stack = new Stack<>();
        int i = 0;
        int j = 0;

        //循环遍历pushV
        for (; i < pushV.length(); i++) {
            //入栈
            stack.push(pushV.charAt(i));

            //循环比较栈顶元素与popV中的j处值
            //瞄一眼栈顶元素如果相同再出栈并j++
            while (!stack.isEmpty() && j < popV.length() && stack.peek() == popV.charAt(j)) {
                stack.pop();
                j++;
            }
        }

        //pushV空后==》看栈中是否有元素
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(isPopOrder2("ABCDE", "ABCDE"));
        System.out.println(isPopOrder2("ABCDE", "EDCBA"));
        System.out.println(isPopOrder2("ABCDE", "DCEBA"));
        System.out.println(isPopOrder2("ABCDE", "ECDBA"));
    }
}
